package com.happyscan138.makefun.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.happyscan138.makefun.R;
import com.happyscan138.makefun.VideoviewPlay;
import com.happyscan138.makefun.View.RoundImageView;
import com.happyscan138.makefun.base.MediaPlayerManager;
import com.happyscan138.makefun.base.ProcessImageView;
import com.happyscan138.makefun.model.ChatMessage;
import com.happyscan138.makefun.more_myself;
import com.happyscan138.makefun.questionDetailed;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/1/17.
 */

public class chatAdapter extends BaseAdapter {
    private Context mContext;
    private List<ChatMessage> data;
    private LayoutInflater inflater;
    //item的最小宽度
    private int mMinWidth;
    //item的最大宽度
    private int mMaxWidth;
    private static final int TYPE_LEFT = 1;
    private static final int TYPE_RIGHT = 0;
    private static final int TYPE_TO_USER_IMG = 2;
    private static final int TYPE_TO_RESPONCE_IMG = 5;
    private static final int TYPE_TO_RESPONCE_VIDEO = 6;
    private static final int TYPE_TO_USER_VIDEO = 3;
    private static final int TYPE_TO_USER_VOICE = 4;
    private ChatMessage bean;
    private String accessy_ley;
    private View animView;
    private final String TAG="详情";


    public int getImgCount() {
        return imgCount;
    }

    public void setImgCount(int imgCount) {
        this.imgCount = imgCount;
    }

    private int imgCount;


    public chatAdapter(Context mContext, List<ChatMessage> data,String accessy_ley) {
        this.mContext = mContext;
        this.data = data;
        this.accessy_ley=accessy_ley;
        //获取屏幕的宽度
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        //最大宽度为屏幕宽度的百分之七十
        mMaxWidth = (int) (outMetrics.widthPixels * 0.7f);
        //最大宽度为屏幕宽度的百分之十五
        mMinWidth = (int) (outMetrics.widthPixels * 0.15f);
        inflater = LayoutInflater.from(mContext);
    }


    @Override
    public int getViewTypeCount() {
        return 7;
    }

    @Override
    public int getItemViewType(int position) {
        if(data.get(position).type==TYPE_LEFT){
            return TYPE_LEFT;
        }else if(data.get(position).type==TYPE_RIGHT) {
            return TYPE_RIGHT;
        }else if(data.get(position).type==TYPE_TO_USER_VIDEO){
            return TYPE_TO_USER_VIDEO;
        }else if(data.get(position).type==TYPE_TO_USER_VOICE){
            return TYPE_TO_USER_VOICE;
        }else if(data.get(position).type==TYPE_TO_RESPONCE_IMG){
            return TYPE_TO_RESPONCE_IMG;
        } else if(data.get(position).type==TYPE_TO_RESPONCE_VIDEO){
            return TYPE_TO_RESPONCE_VIDEO;
        }else {
            return TYPE_TO_USER_IMG;
        }

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        int Type=getItemViewType(position);
        ViewHolder_chat_left viewHolder_chat_left=null;
        ViewHolder_chat_right viewHolder_chat_right=null;
        ViewHolder_chat_right_Img viewHolder_chat_right_img=null;
        ViewHolder_chat_left_Img viewHolder_chat_left_img=null;
        ViewHolder_chat_right_Video viewHolder_chat_right_video=null;
        ViewHolder_chat_left_Video viewHolder_chat_left_video=null;
        ViewHolder_chat_right_Voice viewHolder_chat_right_voice=null;

        inflater=LayoutInflater.from(mContext);

        if(view==null){
            switch (Type){
                case TYPE_LEFT:
                    viewHolder_chat_left=new ViewHolder_chat_left();
                    view=inflater.inflate(R.layout.chat_left_item,parent,false);
                    viewHolder_chat_left.chat_left_head=(RoundImageView)view.findViewById(R.id.chat_left_head);
                    viewHolder_chat_left.chat_message=(TextView)view.findViewById(R.id.chat_left_text);
                    viewHolder_chat_left.chat_url=(TextView)view.findViewById(R.id.chat_left_url);
                    view.setTag(R.id.Tag_Chat_Left,viewHolder_chat_left);
//                    Context.setText(data.get(position).content);
                    break;
                case TYPE_RIGHT:
                    viewHolder_chat_right=new ViewHolder_chat_right();
                    view=inflater.inflate(R.layout.chat_right_item,parent,false);
                    viewHolder_chat_right.chat_rightt_head=(RoundImageView)view.findViewById(R.id.chat_right_head);
                    viewHolder_chat_right.chat_message=(TextView)view.findViewById(R.id.chat_right_text);
                    viewHolder_chat_right.chat_url=(TextView)view.findViewById(R.id.chat_right_url) ;
                    view.setTag(R.id.Tag_Chat_Right,viewHolder_chat_right);
                    break;
                case TYPE_TO_USER_IMG:
                    viewHolder_chat_right_img=new ViewHolder_chat_right_Img();
                    view=inflater.inflate(R.layout.chat_right_msg_item,parent,false);
                    viewHolder_chat_right_img.chat_rightthead=(RoundImageView)view.findViewById(R.id.chat_right_head_img);
                    viewHolder_chat_right_img.chat_img=(ImageView) view.findViewById(R.id.chat_right_img);
                    view.setTag(R.id.Tag_Chat_Right_Img,viewHolder_chat_right_img);
                    break;
                case TYPE_TO_USER_VIDEO:
                    viewHolder_chat_right_video=new ViewHolder_chat_right_Video();
                    view=inflater.inflate(R.layout.chat_right_video_item,parent,false);
                    viewHolder_chat_right_video.chat_rightthead=(RoundImageView)view.findViewById(R.id.chat_right_head_video);
                    viewHolder_chat_right_video.chat_video_img=(ImageView)view.findViewById(R.id.chat_detail_img);
                    viewHolder_chat_right_video.play=(ImageView)view.findViewById(R.id.chat_play_btn);
                    view.setTag(R.id.Tag_Chat_Right_Video,viewHolder_chat_right_video);
                    break;
                case TYPE_TO_USER_VOICE:
                    viewHolder_chat_right_voice=new ViewHolder_chat_right_Voice();
                    view=inflater.inflate(R.layout.chat_recoder_right,parent,false);
                    viewHolder_chat_right_voice.right_head=(RoundImageView)view.findViewById(R.id.icon);
                    viewHolder_chat_right_voice.seconds=(TextView)view.findViewById(R.id.id_recoder_time);
                    viewHolder_chat_right_voice.length=view.findViewById(R.id.id_recoder_lenght);
                    view.setTag(R.id.Tag_Chat_Right_Voice,viewHolder_chat_right_voice);
                    break;
                case TYPE_TO_RESPONCE_IMG:
                    viewHolder_chat_left_img=new ViewHolder_chat_left_Img();
                    view=inflater.inflate(R.layout.chat_left_img_item,parent,false);
                    viewHolder_chat_left_img.chat_lefthead=(RoundImageView)view.findViewById(R.id.chat_left_head_img);
                    viewHolder_chat_left_img.chat_img=(ImageView) view.findViewById(R.id.chat_left_img);
                    view.setTag(R.id.Tag_Chat_Left_Img,viewHolder_chat_left_img);
                    break;
                case TYPE_TO_RESPONCE_VIDEO:
                    viewHolder_chat_left_video=new ViewHolder_chat_left_Video();
                    view=inflater.inflate(R.layout.chat_left_video_item,parent,false);
                    viewHolder_chat_left_video.chat_leftthead_video=(RoundImageView)view.findViewById(R.id.chat_left_head_video);
                    viewHolder_chat_left_video.chat_video_img=(ImageView) view.findViewById(R.id.chat_left_detail_img);
                    viewHolder_chat_left_video.play=(ImageView) view.findViewById(R.id.chat_left_play_btn);
                    view.setTag(R.id.Tag_Chat_Left_Video,viewHolder_chat_left_video);
                    break;
            }

        }else {
            switch (Type){
                case TYPE_LEFT:
                    viewHolder_chat_left=(ViewHolder_chat_left)view.getTag(R.id.Tag_Chat_Left);
                    break;
                case TYPE_RIGHT:
                    viewHolder_chat_right=(ViewHolder_chat_right)view.getTag(R.id.Tag_Chat_Right);
                    break;
                case TYPE_TO_USER_IMG:
                    viewHolder_chat_right_img=(ViewHolder_chat_right_Img)view.getTag(R.id.Tag_Chat_Right_Img);
                    break;
                case TYPE_TO_USER_VIDEO:
                    viewHolder_chat_right_video=(ViewHolder_chat_right_Video)view.getTag(R.id.Tag_Chat_Right_Video);
                    break;
                case TYPE_TO_USER_VOICE:
                    viewHolder_chat_right_voice=(ViewHolder_chat_right_Voice)view.getTag(R.id.Tag_Chat_Right_Voice);
                    break;
                case TYPE_TO_RESPONCE_IMG:
                    viewHolder_chat_left_img=(ViewHolder_chat_left_Img)view.getTag(R.id.Tag_Chat_Left_Img);
                    break;
                case TYPE_TO_RESPONCE_VIDEO:
                    viewHolder_chat_left_video=(ViewHolder_chat_left_Video)view.getTag(R.id.Tag_Chat_Left_Video);
                    break;
            }

        }
        bean=data.get(position);
        switch (Type){
            case TYPE_LEFT:
                    Picasso.with(mContext).load(data.get(position).left_head).into(viewHolder_chat_left.chat_left_head);
                    makeUrl(bean.content,viewHolder_chat_left.chat_message,viewHolder_chat_left.chat_url);
                    break;
            case TYPE_RIGHT:
                Picasso.with(mContext).load(data.get(position).right_head).into(viewHolder_chat_right.chat_rightt_head);
                makeUrl(bean.content,viewHolder_chat_right.chat_message,viewHolder_chat_right.chat_url);
                break;
            case TYPE_TO_USER_IMG:
                Picasso.with(mContext).load(data.get(position).right_head).into(viewHolder_chat_right_img.chat_rightthead);
                if(data.get(position).mongo.endsWith(".jpg")){
                    ProcessImageView processImageView=new ProcessImageView(mContext,null,0);
                    LinearLayout linearLayout=(LinearLayout)view.findViewById(R.id.chat_msg_replace);
                    linearLayout.removeView(viewHolder_chat_right_img.chat_img);
                    linearLayout.addView(processImageView);
                    imgCount=getImgCount();
                    processImageView.setProcess(imgCount);
                    Picasso.with(mContext).load(new File(data.get(position).mongo)).into(processImageView);


                    log.e("imgcount", imgCount+"");
                }else {
                    Picasso.with(mContext).load(data.get(position).mongo).into(viewHolder_chat_right_img.chat_img);
                }
                    break;
            case TYPE_TO_USER_VIDEO:
                log.e("TYPE_TO_USER_VIDEO",data.get(position).mongo);
                Picasso.with(mContext).load(data.get(position).right_head).into(viewHolder_chat_right_video.chat_rightthead);
                Picasso.with(mContext).load(data.get(position).thumb_resource).into(viewHolder_chat_right_video.chat_video_img);
                viewHolder_chat_right_video.play.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent();
                        intent.putExtra("url",data.get(position).mongo);
                        intent.putExtra("Thumb",data.get(position).thumb_resource);
                        intent.setClass(mContext, VideoviewPlay.class);
                        mContext.startActivity(intent);
                    }
                });
                break;
            case TYPE_TO_USER_VOICE:
                log.e("seconds",data.get(position).right_head);
                Picasso.with(mContext).load(data.get(position).right_head).into(viewHolder_chat_right_voice.right_head);
                viewHolder_chat_right_voice.seconds.setText(Math.round(bean.Recordtime)+"\"");
                ViewGroup.LayoutParams lp = viewHolder_chat_right_voice.length.getLayoutParams();
                lp.width = (int) (mMinWidth + (mMaxWidth / 60f) * bean.Recordtime);
                viewHolder_chat_right_voice.length.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (animView != null) {
                            animView.setBackgroundResource(R.mipmap.adj);
                            animView = null;
                        }
                        animView = v.findViewById(R.id.id_recoder_anim);
                        animView.setBackgroundResource(R.drawable.play_anim);
                        AnimationDrawable animation = (AnimationDrawable) animView.getBackground();
                        animation.start();
                        // 播放录音
                        log.e("voice","");
                        MediaPlayerManager.playSound("http://120.25.149.212/wenda/cli-apiaccv1-getmsgresource.mp3?access_key=5thcl8ohlb8oof734m2v84ilg2&mongo_resource_id=590be46296f89dbc068b8728", new MediaPlayer.OnCompletionListener() {

                            public void onCompletion(MediaPlayer mp) {
                                //播放完成后修改图片
                                animView.setBackgroundResource(R.mipmap.adj);
                            }
                        });
                    }
                });
                break;
            case TYPE_TO_RESPONCE_IMG:
                Picasso.with(mContext).load(data.get(position).left_head).into(viewHolder_chat_left_img.chat_lefthead);
                Picasso.with(mContext).load(data.get(position).mongo).into(viewHolder_chat_left_img.chat_img);
                break;
            case TYPE_TO_RESPONCE_VIDEO:
                log.e("TYPE_TO_RESPONCE_VIDEO","TYPE_TO_RESPONCE_VIDEO");
                Picasso.with(mContext).load(data.get(position).left_head).into(viewHolder_chat_left_video.chat_leftthead_video);
                Picasso.with(mContext).load(data.get(position).thumb_resource).into(viewHolder_chat_left_video.chat_video_img);
                viewHolder_chat_left_video.play.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent();
                        intent.putExtra("url",data.get(position).mongo);
                        intent.putExtra("Thumb",data.get(position).thumb_resource);
                        intent.setClass(mContext, VideoviewPlay.class);
                        mContext.startActivity(intent);
                    }
                });
                break;
        }
        return view;
    }
    public void makeUrl(String msg,TextView MsgTextView ,TextView UrlTextView){
        Pattern pattern = Pattern.compile("<(\\{[\\w,\\W]+\\})>");
        Matcher matcher = pattern.matcher(bean.content);
        String UserID = null;
        String addddddddddd=null;
        if (matcher.find()) {                                                 //字符串是否与正则表达式相匹配
            String json =msg.substring(matcher.start() + 1, matcher.end() - 1);   //提取出匹配的json字符串
            Log.e("mylog", json);
            String repStr = matcher.replaceAll("").trim();                           //将匹配字段串替换成空串
            MsgTextView.setText(repStr);
            Pattern pattern1 = Pattern.compile("[\\u4e00-\\u9fa5]+");
            Matcher matcher1 = pattern1.matcher(json);
            if (matcher1.find()) {

                addddddddddd = matcher1.group();
                log.e("mylog",addddddddddd);
                UrlTextView.setText(addddddddddd);
            }
            Pattern pattern2 = Pattern.compile("[1-9]+");
            Matcher matcher2 = pattern2.matcher(json);
            if (matcher2.find()) {
                UserID = matcher2.group();
            }
            final String finalUserID = UserID;
            final String finalAddddddddddd = addddddddddd;
            UrlTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(finalAddddddddddd.equals(TAG)){
                        Intent intent = new Intent();
                        intent.putExtra("problem_id", finalUserID);
                        intent.setClass(mContext, questionDetailed.class);
                        mContext.startActivity(intent);
                    }else {
                        Intent intent = new Intent();
                        intent.putExtra("infoUserID", finalUserID);
                        Log.e("infoUserIDImgClick", finalUserID);
                        intent.setClass(mContext, more_myself.class);
                        mContext.startActivity(intent);
                    }
                }
            });
        }else {
            MsgTextView.setText(bean.content);
            UrlTextView.setText(null);
        }
    }
}
